/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;

#endregion;

namespace DotNetNuke.ModuleToolkit.Common
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ImageUtil Class provides helper methods for the Project designed for manipulating
    /// images.
    /// </summary>
    /// <history>
    /// 	[cnurse]	08/01/2006	created
    ///     [anurse]    08/11/2006  renamed to ImageUtil
    /// </history>
    /// -----------------------------------------------------------------------------
    public static class ImageUtil
    {

        #region Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates a thumbnail version of the specified image
        /// </summary>
        /// <param name="image">The file containing the image</param>
        /// <param name="img">The <see cref="System.Web.UI.WebControls.Image"/> control that contains the image to resize</param>
        /// <param name="maxWidth">The maximum width for the image</param>
        /// <param name="maxHeight">The minimum width for the image</param>
        /// <internal>This method is used internally and while it can be called from user code, it is not designed for that purpose</internal>
        /// <history>
        ///     [anurse]    08/11/2006  documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void CreateThumbnail(FileInfo image, Image img, int maxWidth, int maxHeight)
        {
            if (image.Width > image.Height)
            {
                // Landscape
                if (image.Width > maxWidth)
                {
                    img.Width = maxWidth;
                    img.Height = Convert.ToInt32(image.Height * maxWidth / image.Width);
                }
                else
                {
                    img.Width = image.Width;
                    img.Height = image.Height;
                }
            }
            else
            {
                // Portrait
                if (image.Height > maxHeight)
                {
                    img.Width = Convert.ToInt32(image.Width * maxHeight / image.Height);
                    img.Height = maxHeight;
                }
                else
                {
                    img.Width = image.Width;
                    img.Height = image.Height;
                }
            }

        }

        #endregion

    }
}
